package gui;

import javax.swing.JPanel;
import javax.swing.SpringLayout;
import java.awt.Color;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;

import control.SimpleTimer;

import data.Player;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class GamePanel extends JPanel {
	
	PlayerInfoPane[] playerPanes; 
	private JPanel scorePanel;
	private JPanel captionPanel;
	private JPanel challengeGuiPanel;
	private JLabel captionLabel;
	
	public static final Color FONT_COLOR = Color.BLUE;

	/**
	 * Create the panel.
	 */
	public GamePanel(Player[] activePlayers) {
		
		this.init(activePlayers);

	}
	
	public GamePanel(){}
	
	@Override
	public synchronized void addKeyListener(KeyListener l) {
		for (int i = 0; i < playerPanes.length; i++) {
			playerPanes[i].addKeyListener(l);
		}
		scorePanel.addKeyListener(l);
		captionPanel.addKeyListener(l);
		captionLabel.addKeyListener(l);
		challengeGuiPanel.addKeyListener(l);
		super.addKeyListener(l);
	}
	
	public void init(Player[] activePlayers){
		
		this.removeAll();
		
		challengeGuiPanel = new JPanel();
		challengeGuiPanel.setLayout(new BorderLayout());
		challengeGuiPanel.setBackground(GUI.BGGAME);
		
		scorePanel = new JPanel();
		scorePanel.setBackground(GUI.BGGAME);
		
		
		captionPanel = new JPanel();
		captionPanel.setBackground(GUI.BGGAME);
		setLayout(new MigLayout("", "[100px:n,grow 1][431px,grow 5]", "[24px:30px,grow 10][258px,grow]"));
		add(scorePanel, "cell 0 0 1 2,grow");
		scorePanel.setLayout(new GridLayout(4, 1, 0, 0));
		add(captionPanel, "cell 1 0,grow");
		
		captionLabel = new JLabel("default caption");
		captionLabel.setFont(GUI.font);
		captionLabel.setForeground(FONT_COLOR);
		captionPanel.add(captionLabel);
		add(challengeGuiPanel, "cell 1 1,grow");
		
		playerPanes = new PlayerInfoPane[activePlayers.length];
		for (int i = 0; i < playerPanes.length; i++) {
			playerPanes[i] = new PlayerInfoPane(activePlayers[i]);
			this.scorePanel.add(playerPanes[i]);
		}
		SimpleTimer.pause(100, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				GamePanel.this.updateScores();
				GamePanel.this.repaint();
			}
		});

	}
	
	public void updateScores(){
		for (PlayerInfoPane plP : playerPanes) {
			plP.updateDisplay();
			plP.iHaveBuzzed(false);
		}
		updatePlacings();
	}
	
	public void updatePlacings(){
		int highestScore = Integer.MIN_VALUE;
		for (PlayerInfoPane plP : playerPanes) {
			if(plP.getPlayer().getPoints() > highestScore)highestScore = plP.getPlayer().getPoints();
		}
		for (PlayerInfoPane plP : playerPanes) {
			if(plP.getPlayer().getPoints() == highestScore)plP.setLeader(true);
			else plP.setLeader(false);
		}
	}
	
	public JPanel getChallengePanel(){
		return challengeGuiPanel;
	}
	
	public void setCaption(String caption){
		this.captionLabel.setText(caption);
		//TODO implement resize ability
	}

	public void playerHasPressed(Player pl) {
		for (int i = 0; i < playerPanes.length; i++) {
			if(playerPanes[i].getPlayer().equals(pl))playerPanes[i].iHaveBuzzed(true);
		}
		
	}
}
